package com.centaline.androidsalesblog.overlay;

import android.content.Context;
import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.BaiduMarkerClick;
import com.centaline.androidsalesblog.model.businissobject.StoreBo;

import java.util.ArrayList;
import java.util.List;

/**
 * 门店
 * Created by Lihq on 16/2/19.
 */
public class StoreOverlay implements BaiduMap.OnMarkerClickListener {

    protected final BaiduMap baiduMap;
    protected final BaiduMarkerClick baiduMarkerClick;

    protected ArrayList<StoreBo> data;

    protected List<Overlay> overlayList;

    public StoreOverlay(Context context, BaiduMap baiduMap, BaiduMarkerClick baiduMarkerClick) {
        this.baiduMap = baiduMap;
        this.baiduMarkerClick = baiduMarkerClick;
        if (overlayList == null)
            overlayList = new ArrayList<>();
    }

    /**
     * 设置数据
     */
    public void setData(ArrayList<StoreBo> data) {
        this.data = data;
    }

    /**
     * 将所有Overlay 添加到地图上
     */
    public final void addToMap() {
        if (checkBaiduMap())
            return;
        if (data == null)
            return;
        removeFromMap();
        for (StoreBo storeBo : data) {
            overlayList.add(baiduMap.addOverlay(getMarkerOptions(storeBo)));
        }
    }

    /**
     * 设置MarkerOptions
     */
    public MarkerOptions getMarkerOptions(StoreBo storeBo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ExtraConstant.MAP_EXTRA_INFO, storeBo);
        return new MarkerOptions()
                .position(new LatLng(storeBo.getLpt_Y(), storeBo.getLpt_X()))
                .extraInfo(bundle)
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_store_point_normal));
    }


    /**
     * 移除所有Overlay
     */
    public void removeFromMap() {
        for (Overlay overlay : overlayList) {
            overlay.remove();
        }
        overlayList.clear();
    }



    /**
     * 缩放地图，使所有Overlay都在合适的视野内
     */
    private void zoomToSpan() {
        if (checkBaiduMap()) {
            return;
        }
        if (overlayList.size() > 0) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Overlay overlay : overlayList) {
                // polyline 中的点可能太多，只按marker 缩放
                if (overlay instanceof Marker) {
                    builder.include(((Marker) overlay).getPosition());
                }
            }
            baiduMap.animateMapStatus(MapStatusUpdateFactory
                    .newLatLngBounds(builder.build()));
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return overlayList != null &&
                overlayList.contains(marker) &&
                baiduMarkerClick.onMarkerClick(marker);
    }

    /**
     * 检查BaiduMap
     */
    protected boolean checkBaiduMap() {
        return baiduMap == null;
    }
}
